
import { MapList } from "../../../cfw/struct/MapList";
import { BuffMgr } from "../model/BuffMgr";
import { IBuffModel } from "../model/IBuffModel";
import { IBuffView } from "./IBuffView";
import BaseModel from "../../../cfw/mvc/BaseModel";
import { BaseView } from "../../../cfw/mvc/BaseView";
import { IBattleAttacker } from "../../player/IBattleAttacker";
export abstract class BuffViewMgr<T extends IBattleAttacker, M extends BaseModel> extends BaseView<null, M> {
    protected buffs: MapList<number, IBuffView> = new MapList()
    protected buffer: any[] = []
    protected removeList: IBuffView[] = []

    target: T;

    addListener(): void {
        this.eventProxy.on(BuffMgr.ADD_BUFF, this.addBuff, this)
        this.eventProxy.on(BuffMgr.REMOVE_BUFF, this.removeBuff, this)
    }


    addBuff(buff: IBuffModel) {
        if (this.buffs.has(buff.getID())) {
            return;
        }
        let view = this.createBuff(buff)
        this.buffs.set(buff.getID(), view)
    }

    protected abstract createBuff(buff: IBuffModel): IBuffView;

    removeBuff(buff: IBuffView) {
        if (this.buffs.has(buff.getID())) {
            buff.setVisible(false)
            this.removeList.push(buff)
        }
    }

    updateLogic(dt: number) {
        let list = this.buffs.getList();
        for (let index = 0; index < list.length; index++) {
            const element = list[index];
            element.updateLogic(dt)
            if (element.isFinish()) {
                this.removeBuff(element)
            }
        }
        this.recover();
    }

    recover() {
        while (this.removeList.length > 0) {
            let buff = this.removeList.shift();
            this.buffer.push(buff.node)
            this.buffs.remove(buff.getID(), buff)
        }
    }

}